// ----------------------------------------------------------------
//  Global constatants and variables
// ----------------------------------------------------------------
var map = kartograph.map('#map');


// ----------------------------------------------------------------
//  General event handlers
// ----------------------------------------------------------------

window.onload = function() {
    prepareMap();
};

$(window).resize(resizeMap);

/* 
 * Get data from server
 */
function getData(url) {
	var resp;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", url, false);
	xmlhttp.send();
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        if (xmlhttp.responseText == 'Not found') {
            alert('The url is wrong!');
        } else {
            resp = JSON.parse(xmlHttp.responseText);
        }                    
    }	
	return resp;
}

/* 
 * TODO:
 * Render data
 */
function renderData(data) {
	if (data) {
		// ...
	}
}

// ----------------------------------------------------------------
//  Map
// ----------------------------------------------------------------

function prepareMap() {
    var options = {'valign':'top'};
    map.loadMap('/static/svg/world.svg', initializeMap, options);
}


function initializeMap() {
    var options = {
        styles: {
            fill: getCountryColor,
            cursor: "pointer"
        },
        key: "id",
        click: countryClick
    };
    this.addLayer('countryLayer', options);
}


/*
 * TODO:
 * Calculate fill color based on the amount of received money
 */
function getCountryColor(countryData) {
    return '#eee';
}


function countryClick(data, path, event) {
   visualizeCountryData(data.id);
}


function resizeMap() {
    map.resize();
}

// ----------------------------------------------------------------
//  Visualization of data about country
// ----------------------------------------------------------------

// TODO
function visualizeCountryData(id) {
    $('#country-panel .panel-heading').html(id);
}
